<?php

namespace App\Notifications\OrderNotifications;

use App\Common\Constant\Params;
use App\Models\Master;
use App\Models\Merchant;
use App\Models\Order;
use App\Models\OrderComplain;
use App\Notifications\Channels\SmsChannel;
use App\Notifications\OrderNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class OrderComplainedNotification extends Notification implements OrderNotification
{
    use Queueable;

    private $complain;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(OrderComplain $complain)
    {
        $this->complain = $complain;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        if ($notifiable instanceof Master){
            return ['database'];
        }else if ($notifiable instanceof Merchant){
            return [SmsChannel::class];
        }else{
            return [];
        }
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray(Master $master)
    {
        if ($this->complain->oc_handle_result == Params::COMPLAIN_RESULT_AGREE){//投诉成立
            $message = <<<EOT
%s师傅，您好！您服务的订单%s，经过客服调查核实，处理结果:投诉成立，%s。
EOT;
            $message = sprintf($message,
                $master->real_name,
                $this->complain->order->order_number,
                $this->complain->oc_handle_explain);
        }else{//投诉不成立
            $message = <<<EOT
%s师傅，您好！您服务的订单%s，经过客服调查核实，处理结果:投诉不成立。
EOT;
            $message = sprintf($message,
                $master->real_name,
                $this->complain->order->order_number);
        }
        return [
            'title'=>'投诉处理结果提醒',
            'order_id'=>$this->complain->order->id,
            'message'=>$message
        ];
    }

    /**
     * 向商家发送短信
     * @param Master $master
     * @return array($phoneNum,$message)
     */
    public function toSms(Merchant $merchant)
    {
        if ($this->complain->oc_handle_result == Params::COMPLAIN_RESULT_AGREE){//投诉成立
            $message = <<<EOT
【%s】您投诉%s师傅，客服已处理完成，判定投诉成立，平台将会依据处罚条例对师傅违规处罚。如有疑问请致电0779-3931900
EOT;
        }else{//投诉不成立
            $message = <<<EOT
【%s】您投诉%s师傅，经过客服核实调查，师傅并没有违规，所以您的投诉不成立。如有疑问请致电0779-3931900
EOT;
        }
        $message = sprintf($message,
            config('app.name'),
            $this->complain->order->master->real_name);
        return [$merchant->me_phone,$message];
    }
}
